<?php

use app\exception\ApiException;
use PHPExcel;
use think\admin\model\SystemQueue;
use think\admin\service\QueueService;

// The following code is used to define a DS constant if it does not already exist
if(!defined('DS')){
    define('DS',DIRECTORY_SEPARATOR);
}

function exportExcel($expTableData = [], $expTitle = ''){
    $xlsTitle = $expTitle;//文件名称
    $fileName = $expTitle;//or $xlsTitle 文件名称可根据自己情况设定
    $dataNum = count($expTableData);
    $objPHPExcel = new \PHPExcel();
    $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
    for($i=0;$i<$dataNum;$i++){
        $j = 0;
        foreach ($expTableData[$i] as $key => $value) {
            // $value = is_string($value) ? $value : json_encode($value);
            $tmp_color = false;

            if(is_array($value)){
                if(isset($value['color'])){
                    $tmp_color = $value['color'];
                }
                if(isset($value['content'])){
                    $value = $value['content'];
                }
            }


            $item_index = $cellName[$j].($i + 1);

            if($tmp_color){

                $objPHPExcel->getActiveSheet()->getStyle($item_index)->getFill()->applyFromArray(array(
                    'type' => PHPExcel_Style_Fill::FILL_SOLID,
                    'startcolor' => array(
                            'rgb' => $tmp_color        
                    )
                ));
            }

            $objPHPExcel->getActiveSheet(0)->setCellValueExplicit($item_index, $value, \PHPExcel_Cell_DataType::TYPE_STRING);
            $objPHPExcel->getActiveSheet(0)->getStyle($item_index)->getAlignment()->setWrapText(true);
            $j++;
        }
    }
    $objPHPExcel->getActiveSheet(0)->getDefaultRowDimension()->setRowHeight(20);
    $objPHPExcel->getActiveSheet(0)->getDefaultColumnDimension()->setWidth(18);
    ob_end_clean();
    header('pragma:public');
    header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');
    header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
    $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    $objWriter->save('php://output');
    exit;
}


function getExcelTableData($file_path){

    // 读取Excel文件
    $objPHPExcel = \PHPExcel_IOFactory::load($file_path);
    // 获取第一个工作表
    $sheet = $objPHPExcel->getSheet(0);
    // 获取总行数
    $total_rows = $sheet->getHighestRow();

    // 获取总列数
    $total_columns = $sheet->getHighestColumn();

    // 遍历Excel文件
    $table = [];
    for ($row = 1; $row <= $total_rows; $row++) {
        $data = array();
        for ($column = 'A'; $column <= $total_columns; $column++) {
            $data[] = $sheet->getCell($column . $row)->getValue();
        }
        $table[] = $data;
    }
    return $table;
}


function apiException($message = ''){
    throw new ApiException($message);
}


if (!function_exists('openQueue')) {
    /**
     * 注册异步处理任务
     * @param string $title 任务名称
     * @param string $command 执行内容
     * @param integer $later 延时执行时间
     * @param array $data 任务附加数据
     * @param integer $rscript 任务类型(0单例,1多例)
     * @param integer $loops 循环等待时间
     * @return string
     * @throws \think\admin\Exception
     */
    function openQueue(string $title, string $command, int $later = 0, array $data = [], int $rscript = 1, int $loops = 0): string
    {
        // 这里判断一下有多少个异步任务正在执行中。每多一个异步任务，当前新建的异步任务就往后推90秒才执行
        $count = SystemQueue::whereIn('status', [1, 2])->count();
        $later = $later + $count * 45;
        return QueueService::register($title, $command, $later, $data, $rscript, $loops)->code;
    }
}